package com.ruoyi.travel.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.travel.domain.TravelReview;
import com.ruoyi.travel.service.ITravelReviewService;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 评价Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/travel/review")
public class TravelReviewController extends BaseController
{
    @Autowired
    private ITravelReviewService travelReviewService;

    /**
     * 查询评价列表
     */
    @RequiresPermissions("travel:review:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelReview travelReview)
    {
        startPage();
        List<TravelReview> list = travelReviewService.selectTravelReviewList(travelReview);
        return getDataTable(list);
    }

    /**
     * 小程序查询产品评价列表（无需权限）
     */
    @GetMapping("/app/list")
    public AjaxResult appList(@RequestParam("productId") Long productId)
    {
        List<TravelReview> list = travelReviewService.selectTravelReviewListByProductId(productId);
        return success(list);
    }

    /**
     * 小程序查询产品评价统计（无需权限）
     */
    @GetMapping("/app/stats")
    public AjaxResult appStats(@RequestParam("productId") Long productId)
    {
        Map<String, Object> stats = travelReviewService.selectTravelReviewStatsByProductId(productId);
        return success(stats);
    }

    /**
     * 获取评价详细信息
     */
    @RequiresPermissions("travel:review:query")
    @GetMapping(value = "/{reviewId}")
    public AjaxResult getInfo(@PathVariable("reviewId") Long reviewId)
    {
        return success(travelReviewService.selectTravelReviewById(reviewId));
    }

    /**
     * 小程序获取评价详细信息（无需权限）
     */
    @GetMapping(value = "/app/{reviewId:[0-9]+}")
    public AjaxResult appGetInfo(@PathVariable("reviewId") Long reviewId)
    {
        return success(travelReviewService.selectTravelReviewById(reviewId));
    }

    /**
     * 小程序新增评价（使用当前登录用户信息）
     */
    @PostMapping("/app")
    public AjaxResult appAdd(@RequestBody TravelReview travelReview)
    {
        if (travelReview.getProductId() == null)
        {
            return error("产品信息不能为空");
        }

        LoginUser loginUser = null;
        try
        {
            loginUser = SecurityUtils.getLoginUser();
        }
        catch (Exception ignored)
        {
            // 小程序可能未登录网关，忽略异常
        }

        if (loginUser != null)
        {
            travelReview.setUserId(loginUser.getUserid());
            travelReview.setUserName(loginUser.getUsername());
            if (loginUser.getSysUser() != null && StringUtils.isNotBlank(loginUser.getSysUser().getAvatar()))
            {
                travelReview.setUserAvatar(loginUser.getSysUser().getAvatar());
            }
            travelReview.setCreateBy(loginUser.getUsername());
        }
        else
        {
            if (travelReview.getUserId() == null || StringUtils.isBlank(travelReview.getUserName()))
            {
                return error("用户信息不能为空");
            }
            if (StringUtils.isBlank(travelReview.getCreateBy()))
            {
                travelReview.setCreateBy(travelReview.getUserName());
            }
        }

        if (travelReview.getReviewTime() == null)
        {
            travelReview.setReviewTime(new Date());
        }
        if (StringUtils.isBlank(travelReview.getStatus()))
        {
            travelReview.setStatus("0");
        }
        return toAjax(travelReviewService.insertTravelReview(travelReview));
    }

    /**
     * 新增评价
     */
    @RequiresPermissions("travel:review:add")
    @Log(title = "评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelReview travelReview)
    {
        travelReview.setCreateBy(SecurityUtils.getUsername());
        if (travelReview.getReviewTime() == null) {
            travelReview.setReviewTime(new java.util.Date());
        }
        if (travelReview.getStatus() == null) {
            travelReview.setStatus("0");
        }
        return toAjax(travelReviewService.insertTravelReview(travelReview));
    }

    /**
     * 修改评价
     */
    @RequiresPermissions("travel:review:edit")
    @Log(title = "评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelReview travelReview)
    {
        travelReview.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(travelReviewService.updateTravelReview(travelReview));
    }

    /**
     * 删除评价
     */
    @RequiresPermissions("travel:review:remove")
    @Log(title = "评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reviewIds}")
    public AjaxResult remove(@PathVariable Long[] reviewIds)
    {
        return toAjax(travelReviewService.deleteTravelReviewByIds(reviewIds));
    }
}

